﻿using System;
using CSC.Google;

namespace CSC
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the object with your google api key 
            PageSpeed pageSpeed = new PageSpeed("your-pagespeed-api-key");

            // Set properties where necessairy, only the url is mandatory
            pageSpeed.Locale = SupportedLanguages.English;
            pageSpeed.PrettyPrint = true;
            pageSpeed.Strategy = Strategy.Desktop;
            pageSpeed.Url = "http://www.microsoft.com";

            // If you dont want to send a server side call to the google pagespeed api,
            // you can use this property to go to the google pagespeed webpage and start a test for your url
            string directGoogleUrl = pageSpeed.DirectGoogleUrl;

            // Get the page speed data for the url from the google pagespeed api
            PageSpeedData data = pageSpeed.GetPageSpeedData();

            if (data != null)
            {
                // you can access rule data either by providing the correct enum
                RuleResult rule = data.FormattedResults.RuleResults[Rule.AvoidBadRequests];
                // or by string
                rule = data.FormattedResults.RuleResults["AvoidBadRequests"];


                // Big loop through all the properties
                Console.WriteLine("Global");
                Console.WriteLine("======");
                Console.WriteLine("  Kind: " + data.Kind);
                Console.WriteLine("  Id: " + data.Id);
                Console.WriteLine("  ResponseCode: " + data.ResponseCode);
                Console.WriteLine("  Title: " + data.Title);
                Console.WriteLine("  Score: " + data.Score);
                Console.WriteLine("\r\nVersion " + data.Version);
                Console.WriteLine("=======");
                Console.WriteLine("  Major: " + data.Version.Major);
                Console.WriteLine("  Minor: " + data.Version.Minor);
                Console.WriteLine("\r\nPageStats");
                Console.WriteLine("=========");
                Console.WriteLine("  CssResponseBytes: " + data.PageStats.CssResponseBytes);
                Console.WriteLine("  FlashResponseBytes: " + data.PageStats.FlashResponseBytes);
                Console.WriteLine("  HtmlResponseBytes: " + data.PageStats.HtmlResponseBytes);
                Console.WriteLine("  ImageResponseBytes: " + data.PageStats.ImageResponseBytes);
                Console.WriteLine("  JavascriptResponseBytes: " + data.PageStats.JavascriptResponseBytes);
                Console.WriteLine("  NumberCssResources: " + data.PageStats.NumberCssResources);
                Console.WriteLine("  NumberHosts: " + data.PageStats.NumberHosts);
                Console.WriteLine("  NumberJsResources: " + data.PageStats.NumberJsResources);
                Console.WriteLine("  NumberResources: " + data.PageStats.NumberResources);
                Console.WriteLine("  NumberStaticResources: " + data.PageStats.NumberStaticResources);
                Console.WriteLine("  OtherResponseBytes: " + data.PageStats.OtherResponseBytes);
                Console.WriteLine("  TextResponseBytes: " + data.PageStats.TextResponseBytes);
                Console.WriteLine("  TotalRequestBytes: " + data.PageStats.TotalRequestBytes);
                Console.WriteLine("\r\nFormattedResults");
                Console.WriteLine("================");
                Console.WriteLine("  Locale: " + data.FormattedResults.Locale + "\r\n");
                foreach (var item in data.FormattedResults.RuleResults)
                {
                    Console.WriteLine("  " + item.Key);
                    Console.Write("  ");
                    for (int i = 0; i < (item.Key as string).Length; i++) Console.Write("=");
                    Console.WriteLine();
                    Console.WriteLine("    LocalizedRuleName: " + item.Value.LocalizedRuleName);
                    Console.WriteLine("    RuleScore: " + item.Value.RuleScore);
                    Console.WriteLine("    RuleImpact: " + item.Value.RuleImpact.ToString("#0.00") + "\r\n");
                    if (item.Value.UrlBlocks != null && item.Value.UrlBlocks.Length > 0)
                    {
                        foreach (var urlblock in item.Value.UrlBlocks)
                        {
                            Console.WriteLine("      Header: " + urlblock.Header.ToString());
                            Console.WriteLine("      Format: " + urlblock.Header.Format);
                            if (urlblock.Header.Args != null)
                                foreach (var arg in urlblock.Header.Args)
                                {
                                    Console.WriteLine("      Type: " + arg.Type);
                                    Console.WriteLine("      Value: " + arg.Value);
                                }
                            if (urlblock.Urls != null && urlblock.Urls.Length > 0)
                                foreach (var url in urlblock.Urls)
                                {
                                    Console.WriteLine("      URL Result: " + url.Result.ToString());
                                    Console.WriteLine("      Format: " + url.Result.Format);
                                    if (urlblock.Header.Args != null)
                                        foreach (var arg in url.Result.Args)
                                        {
                                            Console.WriteLine("      Type: " + arg.Type);
                                            Console.WriteLine("      Value: " + arg.Value);
                                        }
                                }
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }
}
